
function RenameSubsSrt() {
    $items = Get-ChildItem $PSScriptRoot | where {! $_.PSIsContainer}
    ForEach ($item in $items) {
        if ($item -match ".*(\.s\d{2}e\d{2}).*\.srt") {
            $tv_ident = $matches[1]
            $tv_regex = "(.*" + $tv_ident + ".*)\.(avi|mkv|mp4)"
            $movies = Get-ChildItem $PSScriptRoot | where {
                (! $_.PSIsContainer) -and ($_ -match $tv_regex)
            }
            switch ($movies.Count) { 
                0   {
                    Write-Host "    Subtitle '" -nonewline; Write-Host "$item" -f red -nonewline
                    Write-Host "' didn't match any movie file."
                } 
                1   {
                    $movie_base = $matches[1]
                    $item_path = Join-Path $PSScriptRoot $item
                    $item_new_name = $movie_base + ".srt"
                    Rename-Item -Path $item_path -NewName $item_new_name
                    Write-Host "    Subtitle '" -nonewline; Write-Host "$item" -f green -nonewline
                    Write-Host "' renamed to match '$movie_base'."
                } 
                default {
                    Write-Host "    Subtitle '" -nonewline; Write-Host "$item" -f yellow -nonewline
                    Write-Host "' matched more than one movie file."
                }
            }
        }
    }
}

RenameSubsSrt



